package com.my.controller;

import com.my.mapper.RightColumnMapper;
import com.my.mapper.RightMapper;
import com.my.pojo.Right;
import com.my.pojo.RightColumn;
import com.my.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.rmi.server.ServerCloneException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/right")
public class RightController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        String result = "";
        SqlSession sqlSession = MyBatisUtil.openSession();
        if (action.equals("preAdd")) {
            RightColumnMapper rightColumnMapper=sqlSession.getMapper(RightColumnMapper.class);
            List<RightColumn>rightColumnList=rightColumnMapper.searcherAllRightColumn();
            if (rightColumnList==null){
                result="error.jsp";
            }else {
                session.setAttribute("allRightColumn",rightColumnList);
                result="right/addRight.jsp";
            }
        }
        if (action.equals("add")) {
            String rightColumnIdStr=req.getParameter("rightColumnId");
            String rightName=req.getParameter("rightName");
            String rightDesc=req.getParameter("rightDesc");
            String url=req.getParameter("url");
            Right right=Right.builder().rightId(Integer.parseInt(rightColumnIdStr)).rightName(rightName).rightDesc(rightDesc).url(url).build();
            RightMapper rightMapper=sqlSession.getMapper(RightMapper.class);
            Boolean existRight = rightMapper.isExistRight(rightName);
            if (existRight ==null){
                rightMapper.addRight(right);
                sqlSession.commit();
                List<Right>list=this.searchAllRightColumnAndRight();
                if (list==null){
                    result="right/showAllRight.jsp";
                }else {
                    session.setAttribute("allRight",list);
                    result="right/showAllRight.jsp";
                }
            }else {
                result="error.jsp";
            }
        }
        if (action.equals("showAllRight")){
            List<Right>list=this.searchAllRightColumnAndRight();
            if (list==null){
                result="error.jsp";
            }else {
                session.setAttribute("allRight",list);
                result="right/showAllRight.jsp";
            }
        }
        resp.sendRedirect(result);
    }

    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resq) throws ServletException , IOException, ServletException {
        this.doGet(req,resq);
    }

    public List searchAllRightColumnAndRight() {
        SqlSession sqlSession = MyBatisUtil.openSession();
        RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
        List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
        RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
        List list3 = new ArrayList();
        for (int i = 0; i < rightColumnList.size(); i++) {
            RightColumn rightColumn = rightColumnList.get(i);
            String rightColumnName = rightColumn.getRightColumnName();
            List<Right> rightList = rightMapper.searchAllRightByRightColumnId(rightColumn.getId());
            List list1 = new ArrayList();
            List list2 = new ArrayList();
            list1.add(rightColumnName);
            list2.add(list1);
            list2.add(rightColumnList);
            list3.add(list2);
        }
        return list3;

    }
}
